/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package estates.validation;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

public class IntValidator implements Validator{



	public IntValidator(){

	}


	@Override
	public void validate(FacesContext context, UIComponent component,
			Object value) throws ValidatorException {

            if(value==null || value.toString().trim().length()==0)return;

            int a = 0;

            try{
		int b = Integer.parseInt(value.toString().trim());
                if(b < 0)a = 2;

            }catch(NumberFormatException e){a = 1;}
            
            if(a == 1){
                FacesMessage msg =
				new FacesMessage("Integer required.",
						"Integer required.");
			msg.setSeverity(FacesMessage.SEVERITY_ERROR);
			throw new ValidatorException(msg);
            }

            if(a == 2){
                FacesMessage msg =
				new FacesMessage("Number must be positive.",
						"Number must be positive.");
			msg.setSeverity(FacesMessage.SEVERITY_ERROR);
			throw new ValidatorException(msg);
            }

	}
}
